<?php

namespace App\Model;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MsgHistory extends BaseModel
{
    protected $table = 'msg_history';
    protected $connection = 'mysql';

    public $timestamps = false;
    private $fields = ['*'];

    public function getCreatedAtAttribute($value)
    {
        if (!$value) return 0;
        return date('Y-m-d H:i:s', $value);
    }

    public function getList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $type = $request->input('type', '1');
        $uuid = $request->input('uuid', 0);
        if ($type == 2) {
            $where[] = ['user_id', '=', '全员'];
        }else if ($uuid){
            $where[] = ['user_id', 'like', '%'.$uuid.'%'];
        } else {
            $where[] = ['user_id', '!=', '全员'];
        }
        $res = DB::table($this->table)->where($where)->orderBy('id', 'desc')->paginate($limit);
        return $this->objToArray($res);
    }


    protected $fillable = ['user_id', 'context', 'created_at', 'updated_at'];

    public function createData($data)
    {
        return DB::connection($this->connection)->table($this->table)->insertGetId($data);
    }

    public function user()
    {
        return $this->belongsTo(Number::class, 'user_id', 'user_id');
    }
}
